package com.zhangqie.dnui.demo1;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Toast;

import com.zhangqie.dnui.R;
import com.zhangqie.dnui.demo1.adapter.Demo4Adapter;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by zhangqie on 2018/8/9.
 * Describe:
 */

public class Demo1_4 extends AppCompatActivity {

    SwipeRefreshLayout swipeRefreshLayout;

    List<String> list = new ArrayList<>();
    RecyclerView recyclerView;
    Demo4Adapter demo4Adapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.demo_a4);
        swipeRefreshLayout= findViewById(R.id.swipe_refreshm);
        recyclerView = findViewById(R.id.recyclre_view);
        initView();


    }

    private void initView(){
        swipeRefreshLayout.setSize(SwipeRefreshLayout.LARGE);
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                new Handler().postDelayed(new Runnable() {
                    public void run() {
                        Toast.makeText(Demo1_4.this,"刷新后的数据",Toast.LENGTH_LONG).show();
                        swipeRefreshLayout.setRefreshing(false);
                       // list.add("刷新的数据");
                       // demo4Adapter.notifyItemInserted(list.size());
                        list.remove(5);
                        demo4Adapter.notifyItemRemoved(5);
                    }
                }, 2000);
            }
        });
        swipeRefreshLayout.setColorSchemeColors(Color.RED,Color.BLUE,Color.GRAY);
        //设置进度条的背景颜色
        swipeRefreshLayout.setProgressBackgroundColorSchemeColor(Color.YELLOW);
        //设置下拉多少距离开始刷新
        swipeRefreshLayout.setDistanceToTriggerSync(70);
        showData();

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(linearLayoutManager);
        demo4Adapter = new Demo4Adapter(this,list);
        recyclerView.setAdapter(demo4Adapter);

    }

    private void showData(){
        for (int i = 0; i < 7; i++) {
            list.add("条目"+i);
        }
    }

}
